/*
 * @Author: DUST
 * @Date: 2023-06-06 17:18:59
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-12 17:28:41
 * @FilePath: \vite-project\src\utils\ForceEndAxios.ts
 * @Description: 
 * undefined
 */
import axios, { InternalAxiosRequestConfig } from "axios";
import QueryString from "qs";
import projectConfig from "@/config";
import nProgress from "nprogress";
import "nprogress/nprogress.css";
import router from "../router";
import md5 from "js-md5";
// import { logout } from "./index";
import { message } from "ant-design-vue";
nProgress.configure({ showSpinner: false });
import sysInfoStore from "@/store/sysInfo";

/**
 * @description: 登出
 */
export const logout = () => {
  router.push("/login");
}

const Axios = axios.create({
  timeout: 10 * 60 * 1000,
  baseURL: import.meta.env.PROD ? projectConfig.baseurl : "/api/",
  responseType: "json",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
  },
  withCredentials: true
})
/**
 * @description: 添加鉴权表头
 * @param {InternalAxiosRequestConfig} config：请求配置
 */
const getHeaders = (config: InternalAxiosRequestConfig) => {
  const timeStamp = Date.now();
  const sign = md5(config.url + "-@-" + timeStamp);
  
  config.headers["timeStamp"] = timeStamp;
  config.headers["sign"] = sign;
  config.headers["public-ip"] = sysInfoStore.ip;

  return config;
}

 /**
  * @description: 请求拦截
  */
 Axios.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  // console.log(config);
  nProgress.start();
  config = getHeaders(config);
  if(config.data instanceof FormData) {}
  else if (config.headers["Content-Type"] == "application/json") {
    console.log(config.data);
  } else if (["get", "put", "post", "patch"].includes(config.method + "")) {
    config.data = QueryString.stringify(config.data);
  }

  return config;
 })

/**
 * @description: 响应拦截
 */ 
Axios.interceptors.response.use(
  res => {
    nProgress.done();
    if(res.data) {
      if(!projectConfig.successCode.includes(res.data?.code)) {
        message.error(res.data.msg)
        return Promise.reject(res);
      } 
    }
    return Promise.resolve(res);
  },
  error => {
    nProgress.done();
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = "请求错误";
          break;
        case 401:
          error.message = "未授权，请登录";
          logout();
          break;
        case 403:
          error.message = "拒绝访问";
          break;
        case 404:
          error.message = `请求地址出错: ${error.response.config.url}`;
          break;
        case 408:
          error.message = "请求超时";
          break;
        case 500:
          error.message = "服务器内部错误";
          break;
        case 501:
          error.message = "服务未实现";
          break;
        case 502:
          error.message = "网关错误";
          break;
        case 503:
          error.message = "服务不可用";
          break;
        case 504:
          error.message = "网关超时";
          break;
        case 505:
          error.message = "HTTP版本不受支持";
          break;
        default:
          error.message = "系统错误";
          break;
      }
    }
    message.error(error.message);
    return Promise.reject(error);
  }
)

export default Axios;